/**
 * Created by malingling on 15-12-16.
 */
app.controller ('MenuCtrl',function($scope,$location) {
    $.ajaxSettings.async = false;
    $.getJSON('../json/people.json',function(data){
        $scope.people = data['people'];
    })
    var all_inform = localStorage.getItem('all_informs');
    $scope.informs = JSON.parse(all_inform) || [];


    var order_persons = all_order_people($scope.informs);
    $scope.people_count = order_persons.count;
    $scope.none_people_count = $scope.people.length - $scope.people_count;
    var people = order_persons.people
    $scope.names = none_order_people(people,$scope.people)

    $scope.sum = sum($scope.informs).toFixed(2)

})


function all_order_people(informs){
    var order_peoples = {};
    var people_count = 0;
    informs.forEach(function(inform){
        if(order_peoples[inform.person] == undefined )
        {
            order_peoples[inform.person] = inform.person;
            people_count += 1;
        }

    })
    return {count:people_count,people:order_peoples};

}

function none_order_people(names,people){
    var none_order_persons = [];
    people.forEach(function(person){
        if(names[person] != person){
            none_order_persons.push(person)
            console.log(person)
        }
    })
    return none_order_persons;
}

function sum (informs){
    var sum = 0;
    informs.forEach(function(inform){
        sum += parseFloat(inform.price);
    })
    return sum
}


